<?php
/**
 * $Id: tooltip.php 838 2011-01-04 10:33:22Z varun $
 * 
 * Tooltip helper (not ready to be used directly)
 * 
 * Copyright 2005 - 2008, Giga Promoters
 * 						  608 Aggarwal Cyber Plaza
 *						  Netaji Subhash Place, Delhi - 110034
 *						  www.gigapromoters.com
 * 
 *
 * @version 			$Rev: 838 $
 * @lastModifiedBy		$LastChangedBy: varun $
 * @lastChanged			$Date: 2011-01-04 16:03:22 +0530 (Tue, 04 Jan 2011) $
 * @copyright 			Giga Promoters (www.gigapromoters.com)
 * 
 */

class TooltipHelper extends AppHelper {
	
	var $name = "Tooltip";
	var $defaultStyle = "cream";
	var $helpers = array('Javascript');
	/**
	 * Interacts with FormHelper and HtmlHelper to generate JS for tooltips
	 *
	 * @param unknown_type $options
	 * @return unknown
	 */
	function generateOutput($options)
	{
		$html = new HtmlHelper();
		$view =& ClassRegistry::getObject('view');
		
		if(!isset($options['id']))
		{
			$options['id'] = "link_".mt_rand();
		}
		if(!isset($options['tooltipStyle']))
		{
			$options['tooltipStyle'] = $this->defaultStyle;
		}
		
		$output = '<script>$(document).ready(function(){
				$("#'.$options['id'].'").qtip({
					   content: "'.$options['tooltip'].'",
					   style: { 
					      name: "'.$options['tooltipStyle'].'"
					   }
					});
			});</script>';
			
		unset($options['tooltip']);
		unset($options['tooltipStyle']);
					
		$return = array();
		$return['output'] = $output;
		$return['options'] = $options;
		return $return;
	}

	function addQtipTooltip($selector,$tooltip,$options = array(),$return = false)
	{
		$configOptions = cr('Tooltip.Default.options');
		if(!is_array($configOptions))
		{
			$configOptions = array();
		}
		$defaultOptions = array('position'=>array(
				'corner'=>array('target'=>'bottomLeft','tooltip'=>"topLeft")),
				'style'=>array('tip'=>"topLeft",'name'=>"blue"),
				'show'=>array('when'=>array('event'=>"focus")),
				'hide'=>array('when'=>array('event'=>"blur")));
		$options = Set::merge($defaultOptions,$configOptions,$options);
		$options['content'] = __($tooltip);
		$optionsJson = $this->Javascript->object($options);
		//even allow jquery function call.
		if(substr($selector,0,1) != "$")
		{
			$selector = "'$selector'";
		}
		$js = "QtipUtility.register($selector,$optionsJson);";
		if($return == false)
		{
			$this->Javascript->startup($js);
		}else
		{
			$js = $this->Javascript->wrapDocumentReady($js);
		}
		
		return $js;
	}

	function helpTextBox($text='',$inContent='',$marker='(?)')
	{
		$this->helpBoxInUse = true;
		$text = '<span style="display:none" class="helpText">'.$text.'</span>';
		$marker = '<span class="helpTextMarker">'.$marker.'</span>';
		return $inContent.$marker.$text;
	}
	function beforeRender()
	{
		$this->Javascript->link('/cms/js/jquery.qtip.js',false);
	}
	function afterRender()
	{
		if(!isset($this->helpBoxInUse) || $this->helpBoxInUse == false)
		{
			return ;
		}
		//@todo remove from here.
		$js = <<<END
			<Script type="text/javascript">
			\$(document).ready(function(){
			\$('.helpTextMarker').each(function(){
				var text = \$(this).next().text();
				$(this).qtip({'content':text});
			})
		})
			</script>
END;
		ClassRegistry::getObject('View')->addScript('helpBoxJs',$js);
	}
}
?>